<?php

$start = microtime(true);
error_reporting(E_ALL);
set_time_limit(0);
ini_set('memory_limit', '512M');

include '.././conf/config.php';
include '.././helper/helper.php';
include 'curl.php';
//phpinfo();
$remote = new cUrl();



$doc = new DOMDocument();

$allNarrators = array();
for ($char = "a"; $char <= "z"; $char++) {
    echo "\n" . 'now writing ' . $char . "\n";
    $nextUrl = 'http://www.alim.org/library/hadith/narratorindex/' . $char;
    while (true) {
        echo $nextUrl . "\n";
        $remote->url = $nextUrl;
        $outPut = $remote->exe();
        @$doc->loadHTML($outPut);
        
        $nextButtonParent = $doc->getElementById('hd_content');
        $images = $nextButtonParent->getElementsByTagName('img');
        
        $topDiv = $doc->getElementById('topDiv');
        $narrators = $topDiv->getElementsByTagName('a'); // radiallanhu 
        foreach ($narrators as $narrator) {
            $allNarrators[] = $narrator->nodeValue;
        }
        
        
        $i = 0;
        foreach ($images as $image) {
            if ($image->getAttribute('src') == 'http://www.alim.org/sites/all/themes/alim/images/next3.jpg') {
                $a = $image->parentNode;
                $nextUrl =  $a->getAttribute('href');
                $i++;
            }
        }
        
        if ($i == 0) {
           break;
        }
    }
    if ($char == "z") {
        echo 'remote data complete' . "\n";
        break;
    }
}

echo "\n" . 'writing on file' . "\n";
$handle = fopen('csv/narrators.csv', 'w');
fwrite($handle, implode("\n", $allNarrators));
fclose($handle);

echo 'done ...........' . "\n";
echo microtime(true) - $start;